package OperationSystem.day1203blocking_queue_阻塞队列;
import java.util.Random;
public class Producers_With_Consumers {//多生产者多消费者模式
    private static CircleQueue queue=new CircleQueue();
    private static class Producers extends Thread{//生产者
        Producers(){
            super("Producers");
        }
        @Override
        public void run() {
            Random random=new Random(20201203);
            while (true){
                try {
                    queue.put(random.nextInt(100));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    private static class Consumers extends Thread{//消费者
        Consumers(){
            super("Consumers");
        }
        @Override
        public void run() {
            try {
                while (true){
                    queue.take();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        for (int i=0;i<10;i++){
            new Producers().start();
        }
        for (int i=0;i<10;i++){
            new Consumers().start();
        }
    }
}
